home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Comunicatii / htttrack / httrack-3.32-2.exe / {app} / src / webhttrack < prev   
Encoding:
Text File  |  2004-05-02  |  4.7 KB  |  153 lines

  1. #!/bin/bash
  2. #
  3. # WebHTTrack launcher script
  4. # Initializes the htsserver GUI frontend and launch the default browser
  5.  
  6. BROWSEREXE=
  7. SRCHBROWSEREXE="x-www-browser www-browser mozilla firefox firebird galeon konqueror opera netscape"
  8. if test -n "${BROWSER}"; then
  9. # sensible-browser will f up if BROWSER is not set
  10. SRCHBROWSEREXE="sensible-browser ${SRCHBROWSEREXE}"
  11. fi
  12. SRCHPATH="/usr/local/bin /usr/share/bin /usr/bin /usr/lib/httrack /usr/local/lib/httrack /usr/local/share/httrack /sw/bin ${HOME}/usr/bin ${HOME}/bin"
  13. SRCHPATH="$SRCHPATH "`echo $PATH | tr ":" " "`
  14. SRCHDISTPATH="/usr/share /usr/local /usr /local /usr/local/share ${HOME}/usr ${HOME}/usr/share /sw ${HOME}/usr/local ${HOME}/usr/share"
  15.  
  16. ###
  17. # And now some famous cuisine
  18.  
  19. function log {
  20. echo "$0($$): $@" >&2
  21. return 0
  22. }
  23.  
  24. function mozillabrowser {
  25. # returns 0, if the browser is mozilla type
  26. echo "$1" | grep -q "mozilla"
  27. [ $? -eq 0 ] && return 0 
  28. echo "$1" | grep -q "netscape"
  29. [ $? -eq 0 ] && return 0 
  30. echo "$1" | grep -q "firebird"
  31. [ $? -eq 0 ] && return 0 
  32. echo "$1" | grep -q "firefox"
  33. [ $? -eq 0 ] && return 0 
  34. return 1;
  35. }
  36.  
  37. function launch_browser {
  38. # launch any browser
  39. browser=$1
  40. url=$2
  41. user_name=`logname`
  42. browser_name=`basename $browser`
  43. [ ! -x $browser ] && echo "$browser not executable" 1>&2 && exit 1;
  44.  
  45. # if it is a mozilla like browser, check if the browser is running and use 
  46. # -remote if needed. Change the URL into openURL($url) too. 
  47. # (thanks to Torsten Werner for the patch)
  48. mozillabrowser $browser
  49. if [ $? -eq 0 ] ; then 
  50.     ps -e --user "$user_name" | grep -q $browser_name
  51.     if [ $? -eq 0 ] ; then
  52.         url="openURL($url,new-window)"
  53.         browser="${browser} -remote"
  54.     fi
  55. fi
  56.  
  57. $browser "$url" &
  58.  
  59. sleep 10 # a little time to create the new process
  60.  
  61. # now wait until the last browser dies
  62. running=1
  63. while [ $running -eq 1 ] ; do    
  64.     sleep 10
  65.     ps -e --user "$user_name" | grep -q $browser_name
  66.     [ $? -gt 0 ] && running=0
  67. done
  68.  
  69. return
  70. }
  71.  
  72. # First ensure that we can launch the server
  73. BINPATH=
  74. for i in ${SRCHPATH}; do
  75.     ! test -n "${BINPATH}" && test -x ${i}/htsserver && BINPATH=${i}
  76. done
  77. for i in ${SRCHDISTPATH}; do
  78.     ! test -n "${DISTPATH}" && test -f "${i}/httrack/lang.def" && DISTPATH="${i}/httrack"
  79. done
  80. test -n "${BINPATH}" || ! log "could not find htsserver" || exit 1
  81. test -n "${DISTPATH}" || ! log "could not find httrack directory" || exit 1
  82. test -f ${DISTPATH}/lang.def || ! log "could not find ${DISTPATH}/lang.def" || exit 1
  83. test -f ${DISTPATH}/lang.indexes || ! log "could not find ${DISTPATH}/lang.indexes" || exit 1
  84. test -d ${DISTPATH}/lang || ! log "could not find ${DISTPATH}/lang" || exit 1
  85. test -d ${DISTPATH}/html || ! log "could not find ${DISTPATH}/html" || exit 1
  86.  
  87. # Locale
  88. HTSLANG="${LC_MESSAGES}"
  89. ! test -n "${HTSLANG}" && HTSLANG="${LC_ALL}"
  90. ! test -n "${HTSLANG}" && HTSLANG="${LANG}"
  91. test -n "${HTSLANG}" && HTSLANG="`echo ${HTSLANG} | cut -c1-2` | tr 'A-Z' 'a-z'"
  92. LANGN=`grep "${HTSLANG}:" ${DISTPATH}/lang.indexes | cut -f2 -d':'`
  93. ! test -n "${LANGN}" && LANGN=1
  94.  
  95. # Find the browser
  96. # note: not all systems have sensible-browser or www-browser alternative
  97. # thefeore, we have to find a bit more if sensible-browser could not be found
  98.  
  99. for i in ${SRCHBROWSEREXE}; do
  100. for j in ${SRCHPATH}; do
  101. if test -x ${j}/${i}; then
  102. BROWSEREXE=${j}/${i}
  103. fi
  104. test -n "$BROWSEREXE" && break
  105. done
  106. test -n "$BROWSEREXE" && break
  107. done
  108. test -n "$BROWSEREXE" || ! log "cound not find any suitable browser" || exit 1
  109.  
  110. # "browse" command
  111. if test "$1" = "browse"; then
  112. launch_browser "${BROWSEREXE}" "file://${HOME}/websites/index.html"
  113. exit $?
  114. fi
  115.  
  116. # Create a temporary filename
  117. TMPSRVFILE="/tmp/.webhttrack.$$.`head -c16 /dev/random | md5sum | cut -f1 -d' '`"
  118. >${TMPSRVFILE} || ! log "cound not create the temporary file ${TMPSRVFILE}" || exit 1
  119. # Launch htsserver binary and setup the server
  120. (${BINPATH}/htsserver "${DISTPATH}/" path "${HOME}/websites" lang "${LANGN}" $@; echo SRVURL=error) > ${TMPSRVFILE}&
  121. # Find the generated SRVURL
  122. SRVURL=
  123. MAXCOUNT=60
  124. while ! test -n "$SRVURL"; do
  125. MAXCOUNT=$[$MAXCOUNT - 1]
  126. test $MAXCOUNT -gt 0 || exit 1
  127. test $MAXCOUNT -lt 50 && echo "waiting for server to reply.."
  128. SRVURL=`grep -E URL= ${TMPSRVFILE} | cut -f2- -d=`
  129. test ! "$SRVURL" = "error" || ! log "could not spawn htsserver" || exit 1
  130. test -n "$SRVURL" || sleep 1
  131. done
  132.  
  133. # Cleanup function
  134. function cleanup {
  135. test -n "$1" && log "nasty signal caught, cleaning up.."
  136. test -f ${TMPSRVFILE} && SRVPID=`grep -E PID= ${TMPSRVFILE} | cut -f2- -d=`
  137. test -n "${SRVPID}" && kill -9 ${SRVPID}
  138. test -f ${TMPSRVFILE} && rm ${TMPSRVFILE}
  139. test -n "$1" && log "..done"
  140. return 0
  141. }
  142.  
  143. # Cleanup in case of emergency
  144. trap "cleanup now; exit" 1 2 3 4 5 6 7 8 9 11 13 14 15 16 19 24 25
  145.  
  146. # Got SRVURL, launch browser
  147. launch_browser "${BROWSEREXE}" "${SRVURL}"
  148.  
  149. # That's all, folks!
  150. trap "" 1 2 3 4 5 6 7 8 9 11 13 14 15 16 19 24 25
  151. cleanup
  152. exit 0
  153.